package com.rambo.xpd.oslib.demo;

import com.rambo.xpd.base.vo.StudentGridlb;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Create by Rambo on 2017/5/24
 **/
public class JdomDemo {

    public static void main(String[] args) throws Exception {
        String path = Thread.currentThread().getContextClassLoader().getResource("demo.xml").getPath();

        SAXBuilder jdomsaxBuilder = new SAXBuilder(false);
        Document doc = jdomsaxBuilder.build(path);
        Element rootElement = doc.getRootElement();

        List<StudentGridlb> studentGridlbList = new ArrayList<>();
        StudentGridlb studentGridlb;
        for (Object classGridlb : rootElement.getChildren("classGridlb")) {
            Element classGridlbEle = (Element) classGridlb;

            for (Object studentGrid : classGridlbEle.getChild("studentGrid").getChildren("studentGridlb")) {
                Element studentGridEle = (Element) studentGrid;
                studentGridlb = new StudentGridlb();
                studentGridlb.setStu_id(studentGridEle.getChildTextTrim("stu_id"));
                studentGridlb.setStu_age(Integer.parseInt(studentGridEle.getChildTextTrim("stu_age")));
                studentGridlb.setStu_name(studentGridEle.getChildTextTrim("stu_name"));
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                studentGridlb.setStu_birthday(format.parse(studentGridEle.getChildTextTrim("stu_birthday")));
                studentGridlbList.add(studentGridlb);
            }
        }

        System.out.println(studentGridlbList.toString());
        XMLOutputter outputter = new XMLOutputter();
        outputter.output(doc, new FileOutputStream(path));
    }
}